/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import com.dairy.database.DatabaseInterface;
import com.dairy.helpers.DatabaseHelper;
import com.dairy.structures.User;
import com.dairy.structures.Milkman;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author tecton
 */
public class UserInfoModifyAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserInfoModifyForm formBean = (UserInfoModifyForm)form;
        DatabaseInterface db = null;
        HttpSession session = request.getSession();
        String userType = session.getAttribute("userType").toString();
        if (userType.equals("user"))
        {
            db = DatabaseHelper.getUserDatabase(servlet);
            User user = new User();
            user.tel = formBean.getTel();
            user.address = formBean.getAddress();
            user.username = session.getAttribute("username").toString();
            user.real_name = formBean.getReal_name();
            if (formBean.getPassword().equals(formBean.getPassword2()) && !formBean.getPassword().equals(""))
            {
                user.password = formBean.getPassword();
            }
            else if (!formBean.getPassword().equals(""))
            {
                request.setAttribute("success", false);
                request.removeAttribute("personalInfo");
                request.setAttribute("personalInfo", user);
                return mapping.findForward(userType);
            }
            if (db.modifyInfo(user))
            {
                request.removeAttribute("personalInfo");
                request.setAttribute("personalInfo", user);
                request.setAttribute("success", true);
            }
            else
            {
                request.setAttribute("success", false);
            }
            return mapping.findForward(userType);
        }
        else if (userType.equals("milkman"))
        {
            db = DatabaseHelper.getMilkmanDatabase(servlet);
            Milkman milkman = new Milkman();
            milkman.tel = formBean.getTel();
            milkman.job_number = Integer.parseInt(session.getAttribute("username").toString());
            if (!formBean.getPassword().equals(""))
            {
                if (formBean.getPassword().equals(formBean.getPassword2()))
                    milkman.password = formBean.getPassword();
                else
                {
                    request.setAttribute("success", false);
                    request.removeAttribute("personalInfo");
                    request.setAttribute("personalInfo", db.getMilkman(Integer.parseInt(session.getAttribute("username").toString())));
                    return mapping.findForward(userType);
                }
            }
            if (db.modifyInfo(milkman))
            {
                request.removeAttribute("personalInfo");
                request.setAttribute("personalInfo", db.getMilkman(Integer.parseInt(session.getAttribute("username").toString())));
                request.setAttribute("success", true);
            }
            else
            {
                request.setAttribute("success", false);
            }
            return mapping.findForward(userType);
        }
        return mapping.findForward(userType);
    }
}
